﻿// This file is part of WifiCC3100 - the Managed Wifi Driver for the TI CC3100
// Hosted on Codeplex: http://cc3100.codeplex.com
// WifiCC3100 is free software licensed under the Apache License 2.0
// © Copyright 2014 ValkyrieTech, LLC
// by Valkyrie-MT

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System;
using System.Collections;
using System.Text;

namespace Networking
{
    public class AccessPoint
    {
        public string Ssid { get; set; }
        public string Key { get; set; }
        public WifiSecurityType Security { get; set; }
        public byte Priority { get; set; }

        //public static implicit operator byte[](AccessPoint c)
        //{
        //    //TODO: convert the AccessPoint to the byte format used in the connect argument

        //    return new byte[0];
        //}
    }

    public class AccessPointResult : AccessPoint
    {

        public byte SignalStrength { get; set; }
        public byte[] MacAddress { get; set; }

        public static AccessPointResult ParseFrom(byte[] raw)
        {
            var result = new AccessPointResult();
            
            try
            {
                if (raw[33] > 32) throw new ArgumentException("Invalid ssid length detected.");

                // ugh, this seems to have a slightly different enumeration from that used in the profiles,
                // for profiles, it seems that 0 = open, 1 = wep, 2 = wpa/wpa2, 3 = WPS_PBC ...
                // but for a scan, is seems to be 0 = open, 1 = wep, 2 = wpa, 3 = wpa2 ...
                // so I have decided to change the networks detected as WPA2 and change them to WPA so that this object
                // can still be used for connecting by just adding the password.  
                // The disadvantage is that you can no longer differentiate between WPA and WPA2, but I think most would find that ok.  

                result.Ssid = raw.DecodeToString(0, raw[32]);
                result.Security = raw[33] == 0x03 ? WifiSecurityType.WPA : (WifiSecurityType)raw[33];
                result.MacAddress = Utility.ExtractRangeFromArray(raw, 34, 6);
                result.SignalStrength = raw[40];
            }
            catch
            {
                Debug.Print("Error parsing Access Point Result.");
                return null;
            }

            return result;
        }

        public static void SortByDescendingSignalStrength(AccessPointResult[] input)
        {
            //Code derived from: http://stackoverflow.com/questions/3738639/sorting-a-listint

            // exchange sort 
            for (int i = 0; i < (input.Length - 1); i++)
            {
                for (int j = (i + 1); j < input.Length; j++)
                {
                    if (input[i].SignalStrength < input[j].SignalStrength)
                    {
                        var temp = input[i];
                        input[i] = input[j];
                        input[j] = temp;
                    }
                }
            }
        }


    }

    /// <summary>
    /// Based on SL_SEC_TYPE
    /// </summary>
    public enum WifiSecurityType : byte
    {
        Open = 0, WEP = 1, WPA = 2, WPS_PBC = 3, WPS_PIN = 4, WPS_ENT = 5, P2P_PBC = 6, P2P_PIN_KEYPAD = 7, P2P_PIN_DISPLAY = 8
    }

}
